1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 import java.awt.Color;
34 import java.awt.Dimension;
35 import java.awt.Graphics;
36 import java.awt.event.MouseEvent;
37 import java.awt.event.MouseListener;
38
39
40
41
42
43
44
45
46
47
48 @SuppressWarnings("serial")
49 public class SortItem extends java.applet.Applet implements Runnable,
50 MouseListener {
51
52
53
54
55 private Thread kicker;
56
57
58
59 int arr[];
60
61
62
63 int h1 = -1;
64
65
66
67 int h2 = -1;
68
69
70
71 String algName;
72
73
74
75 SortAlgorithm algorithm;
76 Dimension initialSize = null;
77
78
79
80
81 void scramble() {
82 initialSize = getSize();
83 int a[] = new int[initialSize.height / 2];
84 double f = initialSize.width / (double) a.length;
85
86 for (int i = a.length; --i >= 0;) {
87 a[i] = (int) (i * f);
88 }
89 for (int i = a.length; --i >= 0;) {
90 int j = (int) (i * Math.random());
91 int t = a[i];
92 a[i] = a[j];
93 a[j] = t;
94 }
95 arr = a;
96 }
97
98
99
100
101
102 void pause() {
103 pause(-1, -1);
104 }
105
106
107
108
109
110 void pause(int H1) {
111 pause(H1, -1);
112 }
113
114
115
116
117
118 void pause(int H1, int H2) {
119 h1 = H1;
120 h2 = H2;
121 if (kicker != null) {
122 repaint();
123 }
124 try {
125 Thread.sleep(20);
126 } catch (InterruptedException e) {
127 }
128 }
129
130
131
132
133 @Override
134 public void init() {
135 String at = getParameter("alg");
136 if (at == null) {
137 at = "BubbleSort";
138 }
139
140 algName = at + "Algorithm";
141 scramble();
142
143 resize(100, 100);
144 addMouseListener(this);
145 }
146
147 @Override
148 public void start() {
149 h1 = h2 = -1;
150 scramble();
151 repaint();
152 showStatus(getParameter("alg"));
153 }
154
155
156
157
158 @Override
159 public void destroy() {
160 removeMouseListener(this);
161 }
162
163
164
165
166
167 @Override
168 public void paint(Graphics g) {
169 int a[] = arr;
170 int y = 0;
171 int deltaY = 0, deltaX = 0, evenY = 0;
172
173 Dimension currentSize = getSize();
174 int currentHeight = currentSize.height;
175 int currentWidth = currentSize.width;
176
177
178
179
180
181
182
183
184
185 if (!currentSize.equals(initialSize)) {
186 evenY = (currentHeight - initialSize.height) % 2;
187 deltaY = (currentHeight - initialSize.height) / 2;
188 deltaX = (currentWidth - initialSize.width) / 2;
189
190 if (deltaY < 0) {
191 deltaY = 0;
192 evenY = 0;
193 }
194 if (deltaX < 0) {
195 deltaX = 0;
196 }
197 }
198
199
200 g.setColor(getBackground());
201 y = currentHeight - deltaY - 1;
202 for (int i = a.length; --i >= 0; y -= 2) {
203 g.drawLine(deltaX + arr[i], y, currentWidth, y);
204 }
205
206
207 g.setColor(Color.black);
208 y = currentHeight - deltaY - 1;
209 for (int i = a.length; --i >= 0; y -= 2) {
210 g.drawLine(deltaX, y, deltaX + arr[i], y);
211 }
212
213 if (h1 >= 0) {
214 g.setColor(Color.red);
215 y = deltaY + evenY + h1 * 2 + 1;
216 g.drawLine(deltaX, y, deltaX + initialSize.width, y);
217 }
218 if (h2 >= 0) {
219 g.setColor(Color.blue);
220 y = deltaY + evenY + h2 * 2 + 1;
221 g.drawLine(deltaX, y, deltaX + initialSize.width, y);
222 }
223 }
224
225
226
227
228 @Override
229 public void update(Graphics g) {
230 paint(g);
231 }
232
233
234
235
236
237
238
239
240 @Override
241 public void run() {
242 try {
243 if (algorithm == null) {
244 algorithm = (SortAlgorithm) Class.forName(algName).newInstance();
245 algorithm.setParent(this);
246 }
247 algorithm.init();
248 algorithm.sort(arr);
249 } catch (Exception e) {
250 }
251 }
252
253
254
255
256
257 @Override
258 public synchronized void stop() {
259 if (algorithm != null) {
260 try {
261 algorithm.stop();
262 } catch (IllegalThreadStateException e) {
263
264 }
265 kicker = null;
266 }
267 }
268
269
270
271
272
273
274
275
276 private synchronized void startSort() {
277 if (kicker == null || !kicker.isAlive()) {
278 kicker = new Thread(this);
279 kicker.start();
280 }
281 }
282
283 @Override
284 public void mouseClicked(MouseEvent e) {
285 showStatus(getParameter("alg"));
286 }
287
288 @Override
289 public void mousePressed(MouseEvent e) {
290 }
291
292
293
294
295 @Override
296 public void mouseReleased(MouseEvent e) {
297 startSort();
298 e.consume();
299 }
300
301 @Override
302 public void mouseEntered(MouseEvent e) {
303 }
304
305 @Override
306 public void mouseExited(MouseEvent e) {
307 }
308
309 @Override
310 public String getAppletInfo() {
311 return "Title: SortDemo \nAuthor: James Gosling 1.17f, 10 Apr 1995 \nA simple applet class to demonstrate a sort algorithm. \nYou can specify a sorting algorithm using the 'alg' attribute. \nWhen you click on the applet, a thread is forked which animates \nthe sorting algorithm.";
312 }
313
314 @Override
315 public String[][] getParameterInfo() {
316 String[][] info = {
317 { "alg", "string",
318 "The name of the algorithm to run. You can choose from the provided algorithms or suppply your own, as long as the classes are runnable as threads and their names end in 'Algorithm.' BubbleSort is the default. Example: Use 'QSort' to run the QSortAlgorithm class." }
319 };
320 return info;
321 }
322 }